package com.jokerpan.project.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jokerpan.project.commons.PaginationBuilder;
import com.jokerpan.project.entity.BackFiles;
import com.jokerpan.project.mapper.BackFilesMapper;
import com.jokerpan.project.service.BackFilesService;
import com.jokerpan.project.service.MinIoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author jokerpan
 */
@Service("backFilesService")
public class BackFilesServiceImpl implements BackFilesService {

    @Resource
    BackFilesMapper backFilesMapper;
    @Resource
    MinIoService minIoService;

    @Override
    public Map<String, Object> queryList(BackFiles backFiles) {
        Page<Object> page = PageHelper.startPage(backFiles.getCurrentPage(), backFiles.getPageSize());
        List<BackFiles> resultList = this.backFilesMapper.queryList(backFiles);
        return PaginationBuilder.buildResult(resultList, page.getTotal(), backFiles.getCurrentPage(), backFiles.getPageSize());
    }

    @Override
    public boolean add(BackFiles backFiles) {
        return backFilesMapper.add(backFiles) > 0;
    }

    @Override
    public boolean deleteBySourceAndPk(BackFiles backFiles) {
        return backFilesMapper.deleteBySourceAndPk(backFiles) > 0;
    }

    @Override
    public boolean deleteByUrl(String url) {
        return backFilesMapper.deleteByUrl(url) > 0;
    }

    @Override
    public boolean deleteFile(String url) {
        backFilesMapper.deleteByUrl(url);
        minIoService.deleteFile(url);
        return true;
    }
}
